package com.example.towSpringBoot.controller;


import com.example.towSpringBoot.annotation.RequiredPermission;
import com.example.towSpringBoot.common.PageQuery;
import com.example.towSpringBoot.common.PageResult;
import com.example.towSpringBoot.common.Result;
import com.example.towSpringBoot.dto.DepartmentDto;
import com.example.towSpringBoot.entity.Department;
import com.example.towSpringBoot.service.DepartmentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xinTong
 * @since 2024-01-23
 */
@RestController
@RequestMapping("api/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 部门查询
     * @param
     * @return
     */
    @GetMapping("/list")
    @RequiredPermission(name = "部门列表",expression = "department:list")
    public Result query(@RequestParam(value = "pageNum")int pageNum,
                        @RequestParam(value = "pageSize")int pageSize){

        PageResult pageResult = departmentService.query(pageNum, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 查询所有部门
     * @return
     */
    @GetMapping("/listAll")
    @RequiredPermission(name = "部门列表",expression = "department:list")
    public Result<Department> listAll(){
        List<Department> list = departmentService.listAll();
        return Result.success(list);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    @RequiredPermission(name = "部门删除",expression = "department:delete")
    public Result delete(@PathVariable Long id){
        departmentService.delete(id);
        return Result.success();
    }

    /**
     * 查询指定部门
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result list(@PathVariable Long id){

        DepartmentDto dto = departmentService.info(id);
        return Result.success(dto);
    }

    /**
     * 新增或修改
     * @param departmentDto
     */
    @PostMapping("saveOrUpdate")
    @RequiredPermission(name = "部门保存或更新",expression = "department:saveOrUpdate")
    public Result saveOrUpdate(@RequestBody DepartmentDto departmentDto){
        departmentService.saveOrUpdate(departmentDto);
        return Result.success();
    }
}
